package com.qianyu.project.web.controller.wyh;

import com.qianyu.project.entity.PaymentList;
import com.qianyu.project.entity.SubSettle;
import com.qianyu.project.entity.SubSettleList;
import com.qianyu.project.entity.SubcontractProgressPayment;
import com.qianyu.project.service.ILaborSubcontractService;
import com.qianyu.project.service.ISubSettleListService;
import com.qianyu.project.service.ISubSettleService;
import com.qianyu.project.utils.Result;
import com.qianyu.project.utils.ResultTool;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * author wyh
 * Date 2021/2/9
 **/
@RestController
@RequestMapping("WyhSubSettle")
public class WyhSubSettleController {

    @Autowired
    private ISubSettleService iSubSettleService;

    @Autowired
    private ISubSettleListService iSubSettleListService;

    @GetMapping("/code")
    public Result getMaxCode() {
        return ResultTool.success(Long.parseLong(iSubSettleService.getMaxCode()) + 1);
    }

    @PostMapping("")
    public Result addLaborSubcontract(@RequestBody Map<String,Object> adds){
        System.out.println(adds);
        SubSettle ss = new SubSettle();
        Map<String,Object> mapls = (Map<String, Object>) adds.get("subcontracts");
        ss.setJsId(Integer.valueOf(mapls.get("jsId")+""));
        ss.setConId(Integer.valueOf(mapls.get("conId")+""));
        ss.setPId(Integer.valueOf(mapls.get("pId")+""));
        ss.setProId(Integer.valueOf(mapls.get("proId")+""));
        ss.setJsName(mapls.get("jsName")+"");
        ss.setContractAmount(BigDecimal.valueOf(Double.valueOf(mapls.get("contractAmount")+"")));
        ss.setEId(Integer.valueOf(mapls.get("eId")+""));
        ss.setPenalty(BigDecimal.valueOf(Double.valueOf(mapls.get("penalty")+"")));
        ss.setWithhold(BigDecimal.valueOf(Double.valueOf(mapls.get("withhold")+"")));
        ss.setSettleAmount(BigDecimal.valueOf(Double.valueOf(mapls.get("settleAmount")+"")));
        ss.setAgreeUpper(mapls.get("agreeUpper")+"");
        ss.setSettleComment(mapls.get("settleComment")+"");
        ss.setAttachIds(mapls.get("attachIds")+"");
        ss.setApproveStatus(Integer.valueOf(1+""));
        ss.setCreateDate(mapls.get("createDate")+"");
        iSubSettleService.saveOrUpdate(ss);
        List<Map<String,Object>> maptpd = (List<Map<String, Object>>) adds.get("tenderPurchaseDetails");
        List<SubSettleList> aiList = Lists.newArrayList();
        maptpd.forEach(i->{
            SubSettleList qq = new SubSettleList();
            qq.setSubList(i.get("subList")+"");
            qq.setUnit(i.get("unit")+"");
            qq.setWorkCount(Integer.valueOf(i.get("workCount")+""));
            qq.setComperPrice(BigDecimal.valueOf(Double.valueOf(20+"")));
            qq.setWcCount(Integer.valueOf(i.get("wcCount")+""));
            qq.setZxPrice(BigDecimal.valueOf(Double.valueOf(i.get("zxPrice")+"")));
            qq.setSumPrice(BigDecimal.valueOf(Double.valueOf(i.get("sumPrice")+"")));
            qq.setSubId(ss.getId());
            aiList.add(qq);
        });
        iSubSettleListService.saveBatch(aiList);
        return ResultTool.success();
    }

    @GetMapping("/list")
    public Result getSubcontracts(@RequestParam Map<String,Object> map){

        return ResultTool.success(iSubSettleService.getSubcontracts(map));
    }

}
